package cn.aifeix.controller;

import cn.aifeix.block.UserBlockHandler;
import cn.aifeix.domain.User;
import cn.aifeix.fallback.UserFallback;
import cn.aifeix.feign.UserService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private UserService userService;

    /**
     * value 指定当前接口的资源名称，就是别名
     * blockHandler 指定触发限流后响应的托底数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @SentinelResource(value = "getUserById",blockHandler = "getUserByIdBlockHandler",
            blockHandlerClass = UserBlockHandler.class,
            fallback = "getUserByIdFallback",
            fallbackClass = UserFallback.class)
    public User getUserById(@PathVariable("id") Long id) {
//        int a = 1/0;
        return userService.getUserById(id);
    }

//    public User getUserByIdBlockHandler(@PathVariable("id") Long id, BlockException e) {
//        e.printStackTrace();
//        return new User(-1L,"限流了","限流了，别来了");
//    }

//    public User getUserByIdFallback(@PathVariable("id") Long id, Throwable e) {
//        e.printStackTrace();
//        return new User(-1L,"熔断了","熔断了，别搞了");
//    }
}
